/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      TimeProfiler.hpp
 *
 * $FTS$
 */
#ifndef _TIME_PROFILER_HPP__
#define _TIME_PROFILER_HPP__ 1

/**
  @file TimeProfiler.hpp
  @brief Time profiler for measueraments of execution time
*/

// C Includes
#include <sys/time.h>
#include <string.h>
// Linux stupidity
#ifdef linux
  #include <time.h>
#endif

// Local includes
#include <Hash.hpp>
#include <HashFunctions.hpp>

namespace FTSS // FullTextSearchSystem
{

/**
  @class TimeProfiler TimeProfiler.hpp <TimeProfiler.hpp>
  @brief Time profiler for measueraments of execution time
*/
class TimeProfiler
{
private:
	/**
	  @struct TimeDelta TimeProfiler.hpp <TimeProfiler.hpp>
	  @brief time interval
	*/
	struct TimeDelta
	{
		/** Start time of event */
		struct timeval   tm_begin;
		/** End time of event   */
		struct timeval   tm_end;
		/** Duration of event   */
		struct timeval   tm_delta;
	};

	/**
	  @typedef Pair<TokenHash_t, TimeDelta> TimePair
	  @brief Index pair type definition
	*/
	typedef Pair<TokenHash_t, TimeDelta> TimePair;

	/**
	  @typedef Hash<TokenHash_t, TimeDelta, HashHasher, HashComparator> TimeHash
	*/
	typedef Hash<TokenHash_t, TimeDelta, HashHasher, HashComparator> TimeHash;

	/** Index hash          */
	TimeHash       oTimeHash;
public:
	/**
	  @brief Constructor
	  @param iElements - initial number of elements in hash
	*/
	inline TimeProfiler(INT_32 iElements): oTimeHash(iElements) { ;; }

	/**
	  @brief Start point of event (overloaded method)
	  @param sPointName - name of event
	  @return 0 if this point is new time pooint and -1 if this point exist
	*/
	inline INT_32 StartPoint(CCHAR_P sPointName) { return StartPoint(sPointName, strlen(sPointName)); }

	/**
	  @brief Start point of event (overloaded method)
	  @param sPointName - name of event
	  @param iPointNameLength - length of point name
	  @return 0 if this point is new time point and -1 if this point exist
	*/
	INT_32 StartPoint(CCHAR_P sPointName, Size_t iPointNameLength);	

	/**
	  @brief End point of event (overloaded method)
	  @param sPointName - name of event
	  @return full time of event execution in milliseconds
	*/
	inline W_FLOAT EndPoint(CCHAR_P sPointName) { return EndPoint(sPointName, strlen(sPointName)); }

	/**
	  @brief End point of event (overloaded method)
	  @param sPointName - name of event
	  @param iPointNameLength - length of point name
	  @return full time of event execution in milliseconds
	*/
	W_FLOAT EndPoint(CCHAR_P sPointName, Size_t iPointNameLength);

	/**
	  @brief Get total time of event execution
	  @param sPointName - name of event
	  @return full time of event execution in milliseconds
	*/
	inline W_FLOAT CheckPoint(CCHAR_P sPointName)
	{
		TokenHash_t iHash = HashFunc(sPointName, strlen(sPointName));
		TimeHash::iterator ithData = oTimeHash.find(iHash, iHash);
		if (ithData != oTimeHash.end())
		{
			return (ithData -> second.tm_delta.tv_sec  * 1000.0 +
			        ithData -> second.tm_delta.tv_usec / 1000.0);
		}
		return -1.0;
	}

	/**
	  @brief Get total time of event execution
	  @param sPointName - name of event
	  @param iPointNameLength - event name length
	  @return full time of event execution in milliseconds
	*/
	inline W_FLOAT CheckPoint(CCHAR_P sPointName, Size_t iPointNameLength)
	{
		TokenHash_t iHash = HashFunc(sPointName, iPointNameLength);
		TimeHash::iterator ithData = oTimeHash.find(iHash, iHash);
		if (ithData != oTimeHash.end())
		{
			return ((ithData -> second.tm_end.tv_sec  - ithData -> second.tm_begin.tv_sec ) * 1000.0 +
			        (ithData -> second.tm_end.tv_usec - ithData -> second.tm_begin.tv_usec ) / 1000.0);
		}
		return -1.0;
	}

	/**
	  @brief Delete event from event list
	  @param sPointName - name of event
	*/
	inline void DeletePoint(CCHAR_P sPointName)
	{
		TokenHash_t iHash = HashFunc(sPointName, strlen(sPointName));
		oTimeHash.erase(iHash, iHash);
	}

	/**
	  @brief Clear event list
	*/
	inline void Clear()
	{
		if (oTimeHash.size() != 0) { oTimeHash.clear(); }
	}
};

} // namespace FTSS
#endif // _TIME_PROFILER_HPP__
// End.
